﻿Imports Mystery

Public Class SingleReference
    Implements IPropertyControl, IContentModificationsSubriver


    Private _property_info As System.Reflection.PropertyInfo
    Private _content_property As ContentReference

    Private _ref As IContent

    Public Sub New()

        ' This call is required by the designer.
        InitializeComponent()

        ' Add any initialization after the InitializeComponent() call.
        myEditIcon.todo = AddressOf referenceChangeRequest

        getGlobalObject(Of ContentModifications).subscrive(Me)

    End Sub

    Public Property property_info As System.Reflection.PropertyInfo Implements IPropertyControl.property_info
        Get
            Return _property_info
        End Get
        Set(value As System.Reflection.PropertyInfo)
            _content_property = value.getMysteryAttribute(Of ContentReference)()
            _label = _content_property.label
            _property_info = value
        End Set
    End Property


    Private _content_button As Button

    Public Property value As Object Implements IPropertyControl.value
        Get
            Return _ref
        End Get
        Set(value As Object)
            If TypeOf (value) Is ContentPropertyValue Then value = DirectCast(value, ContentPropertyValue).getValue
            Dim new_value As IContent = value
            If new_value Is Nothing AndAlso _ref Is Nothing Then Exit Property
            If new_value IsNot Nothing AndAlso new_value.Equals(_ref) Then Exit Property
            _ref = new_value
            'interfance
            myPanel.Children.Clear()
            If _ref IsNot Nothing Then
                _content_button = New Button
                Dim icb As IContentButton = _ref.GetType.getMysteryAttribute(Of ContentButtonAttribute).createButton(_ref)
                AddHandler _content_button.Click, AddressOf icb.Press
                _content_button.Content = icb.getText
                _content_button.MinWidth = 100
                Dim image As Image = icb.getImage
                image.Height = myPanel.Height
                image.Width = myPanel.Height
                myPanel.Children.Add(image)
                myPanel.Children.Add(_content_button)
            End If
            RaiseEvent valueChanged()
        End Set
    End Property


    Private Sub ContentSelected(content As IContent)
        Me.value = content
    End Sub

    Private Sub referenceChangeRequest()
        Dim controler As IContentSelectionControler = _property_info.getMysteryAttribute(Of ContentSelectionControlerAttr).createControler(_content)
        Dim w As New ContentSelectionWindow
        w.Controler = controler
        AddHandler w.contentSelected, AddressOf ContentSelected
        Dim screen_coo As Point = myEditIcon.PointToScreen(New Point)
        w.Left = screen_coo.X
        w.Top = screen_coo.Y
        w.ShowDialog()
        RemoveHandler w.contentSelected, AddressOf ContentSelected
    End Sub

    Public Property typed_value As IContent
        Get
            Return value
        End Get
        Set(value As IContent)
            Me.value = value
        End Set
    End Property

    Private _content As IContent

    Public Property content_item As Mystery.IContent Implements IPropertyControl.content
        Get
            Return _content
        End Get
        Set(value As Mystery.IContent)
            _content = value
            Me.value = _content_property.retrive()(_content)
        End Set
    End Property


    Private _label As String
    Public ReadOnly Property label As String Implements IPropertyControl.label
        Get
            Return _label
        End Get
    End Property


    Public ReadOnly Property wished_height As System.Windows.GridLength Implements IPropertyControl.wished_height
        Get
            Return GridLength.Auto
        End Get
    End Property

    Public Function setInContent() As IEnumerable(Of IContent) Implements IPropertyControl.setInContent
        Dim pv As BaseContentReference = _content_property.retrive()(_content)
        pv.setValue(typed_value)
        Return New List(Of IContent) From {_content}
    End Function

    Public Sub notificate() Implements Mystery.IContentModificationsSubriver.notificate
        If _ref Is Nothing Then Exit Sub
        If _content_button Is Nothing Then Exit Sub
        Me.Dispatcher.Invoke(Sub()
                                 Dim icb As IContentButton = _ref.GetType.getMysteryAttribute(Of ContentButtonAttribute).createButton(_ref)
                                 _content_button.Content = icb.getText
                             End Sub)
    End Sub

    Public ReadOnly Property subscribed As Mystery.IContent Implements Mystery.IContentModificationsSubriver.subscribed
        Get
            Return _ref
        End Get
    End Property

    Public Event valueChanged() Implements IPropertyControl.valueChanged
End Class
